<?php

namespace app\common\model;

use think\Log;
use think\Model;

/**
 * 会员余额日志模型
 */
class MoneyLog extends Model
{

    // 表名
    protected $name = 'user_money_log';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
        'createtime_text'
    ];
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    /**
     * @author LK
     * @date: 2023-10-23 
     * @param object $order  待结算佣金明细
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static function frozenlog_commission($order)
    {
        if ($order['type'] != '1') {
            return true;
        }

        if ($order->pay_money <= 0) {
            return true;
        }

        $pay_money = $order->pay_money;
        $user = User::get($order->user_id);
        if (!$user) {
            return true;
        }
        bcscale(2);
        //直推
        $pUser = User::get($user->pid);
        if ($pUser) {
            $direct = 0;
            $direct_thrust = 0;
            $group = UserGroup::get($pUser['group_id']);
            if ($group) {
                if ($order->is_first == 1) {
                    if ($group->first_direct_thrust > 0) {
                        $direct_thrust = $group->first_direct_thrust / 100;
                    }
                } else {
                    if ($group->direct_thrust > 0) {
                        $direct_thrust = $group->direct_thrust / 100;
                    }
                }
                if ($direct_thrust > 0) {
                    $direct = bcmul($pay_money, $direct_thrust);
                }
                if ($direct > 0) {
                    Frozenlog::money($direct, $user->pid, '直推奖', $order->id);
                }
            }

            //间推
            if ($pUser->pid > 0) {
                $tUser = User::get($pUser->pid);
                if ($tUser) {
                    $interposition = 0;
                    $tgroup = UserGroup::get($tUser['group_id']);
                    if ($tgroup->interposition > 0) {
                        $jt = $tgroup->interposition / 100;
                        $interposition = bcmul($pay_money, $jt);
                    }
                    if ($interposition > 0) {
                        Frozenlog::money($interposition, $tUser->pid, '间推奖', $order->id);
                    }
                }
            }
        }
        //分红奖
        $director = User::whereIn("id", $user->paren_ids)->where('group_id', 4)->order('id desc')->find();
        if ($director) {
            $dividend = 0;
            $dgroup = UserGroup::get($director['group_id']);
            if ($dgroup->dividend > 0) {
                $fh = $dgroup->dividend / 100;
                $dividend = bcmul($pay_money, $fh);
            }
            $bonus = bcmul($pay_money, $dividend);
            if ($bonus > 0) {
                Frozenlog::money($bonus, $director->pid, '分红奖', $order->id);
            }
        }
        return true;
    }

    /**
     * @author LK
     * @date: 2023-10-23 
     * @param array $order  订单分佣
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static function commission($orders)
    {
        $time = time();
        $auto_settlement = config('site.auto_settlement');
        try {
            foreach ($orders as $k => $v) {
                $settletime = $v->taketime + $auto_settlement * 24 * 60 * 60;
                if ($time >= $settletime) {
                    $direct = Frozenlog::get(['order_id' => $v['id'], 'memo' => '直推奖']);
                    if ($direct) {
                        User::money($direct->money, $direct->user_id, '直推奖', $direct->order_id);
                        $direct->is_settle = 1;
                        $direct->save();
                    }

                    $interposition = Frozenlog::get(['order_id' => $v['id'], 'memo' => '间推奖']);
                    if ($interposition) {
                        User::money($interposition->money, $interposition->user_id, '间推奖', $interposition->order_id);
                        $interposition->is_settle = 1;
                        $interposition->save();
                    }
                    $bonus = Frozenlog::get(['order_id' => $v['id'], 'memo' => '分红奖']);
                    if ($bonus) {
                        User::money($bonus->money, $bonus->user_id, '分红奖', $bonus->order_id);
                        $bonus->is_settle = 1;
                        $bonus->save();
                    }
                    $v->is_settle = 1;
                    $v->save();
                }
            }
        } catch (\Exception $e) {
            exception($e->getMessage());
        }
        return true;
    }
}
